package com.czwj.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.DataGridData;
import com.czwj.entities.Notice;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.PlanMain;
import com.czwj.entities.ProjectItem;
import com.czwj.entities.UserInfo;
import com.czwj.entities.WarningValue;
import com.czwj.service.NoticeService;
import com.czwj.service.PlanMainService;
import com.czwj.service.ProjectItemService;
import com.czwj.service.WarningValueService;
import com.czwj.util.TextUtil;

@Controller
public class NoticeAction {
	@Resource
	ProjectItemService projectItemService;
	@Resource
	PlanMainService planMainService;
	@Resource
	NoticeService noticeService;
	@Resource
	WarningValueService warningValueService;
	
	@RequestMapping(value = "/getnotice.do", method = RequestMethod.GET)
	@ResponseBody
	public List<Notice> getnotice(){
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		List<Notice> list = noticeService.searchbyuser(userInfo.getId());
		return list;
	}
	
	@RequestMapping(value = "/clearnotice.do", method = RequestMethod.GET)
	@ResponseBody
	public void clearnotice(){
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		List<Notice> list = noticeService.searchbyuser(userInfo.getId());
		for (Notice notice : list) {
			notice.setStatus(1);
			noticeService.updateByPrimaryKeySelective(notice);
		}
	}
	
	@SystemControllerLog(description = "消息已读")
	@RequestMapping(value = "noticeDelete.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult forumPostDelete(@RequestParam("id") int id){
		OperateResult result = new OperateResult();
		Notice notice = noticeService.selectByPrimaryKey(id);
		notice.setStatus(1);
		int count = noticeService.updateByPrimaryKeySelective(notice);
		result.setSuccess(count > 0);
		return result;
	}
	
	@RequestMapping(value = "/addnotice.do", method = RequestMethod.POST)
	@ResponseBody
	public void addnotice(){
		DataGridData<PlanMain> result = new DataGridData();
		Map<String, Object> map = new HashMap<String, Object>();
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo user=((UserInfo)currentUser.getSession().getAttribute("userInfo"));
		Map<String, Object> map2 = new HashMap<String, Object>();
		List<PlanMain> planlist=new ArrayList<PlanMain>();
		List<PlanMain> planmainlist=new ArrayList<PlanMain>();
		if(user.getRoleid()==1){
			map.put("userid", user.getId());
			map.put("status", 2);
			List<ProjectItem> list = projectItemService.searchforlevel0(map);
			for(int i=0;i<list.size();i++){
				map2.put("projectitemid", list.get(i).getId());
				map2.put("status", 4);
				planlist = planMainService.searchbyprojectitem(map2);
				for (PlanMain planMain : planlist) {
					if (planMain!=null) {
						planmainlist.add(planMain);
					}
				}
			}
		}else if(user.getRoleid()==2){
			map.put("userid", user.getId());
			map.put("status", 2);
			List<ProjectItem> list = projectItemService.searchforlevel1(map);
			for(int i=0;i<list.size();i++){
				map2.put("projectitemid", list.get(i).getId());
				map2.put("status", 4);
				planlist = planMainService.searchbyprojectitem(map2);
				for (PlanMain planMain : planlist) {
					if (planMain!=null) {
						planmainlist.add(planMain);
					}
				}
			}
		}else if (user.getRoleid()==3) {
			map.put("userid", user.getId());
			map.put("status", 2);
			List<ProjectItem> list = projectItemService.searchforlevel2(map);
			for(int i=0;i<list.size();i++){
				map2.put("projectitemid", list.get(i).getId());
				map2.put("status", 4);
				planlist = planMainService.searchbyprojectitem(map2);
				for (PlanMain planMain : planlist) {
					if (planMain!=null) {
						planmainlist.add(planMain);
					}
				}
			}
		}else{
			map.put("userid", user.getId());
			map.put("status", 2);
			List<ProjectItem> list = projectItemService.searchforlevel3(map);
			for(int i=0;i<list.size();i++){
				map2.put("projectitemid", list.get(i).getId());
				map2.put("status", 4);
				planlist = planMainService.searchbyprojectitem(map2);
				for (PlanMain planMain : planlist) {
					if (planMain!=null) {
						planmainlist.add(planMain);
					}
				}
			}
		}
		WarningValue warningValue=warningValueService.selectByPrimaryKey(1);
		List<PlanMain> planmainlist2=new ArrayList<PlanMain>();
		for(int k=0;k<planmainlist.size();k++){
			Double percent=(double) (System.currentTimeMillis()-planmainlist.get(k).getStartdate().getTime())/
					(planmainlist.get(k).getEnddate().getTime()-planmainlist.get(k).getStartdate().getTime());
			
			if(percent*100>=warningValue.getPercent()){
				planmainlist2.add(planmainlist.get(k));
			}
		}
		for(PlanMain planMain:planmainlist2){
			Notice notice = new Notice();
			ProjectItem projectItem = projectItemService.selectByPrimaryKey(planMain.getProjectitemid());
			notice.setTargetid(projectItem.getCreaterid());
			notice.setType(0);
			notice.setContent(projectItem.getName()+"的"+planMain.getName()+"超期");
		}
		
	}
}
